class MyRunnable implements Runnable {
    public volatile boolean isQuit = false;
    @Override
    public void run() {
        while (!isQuit) {
            System.out.println("线程运行中");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("线程结束");
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread t = new Thread(myRunnable);
        t.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("控制线程结束");
        myRunnable.isQuit = true;
    }
}
